package com.wondertek.service.service.business.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wondertek.common.exception.AppException;
import com.wondertek.common.util.base.StringUtil;
import com.wondertek.common.util.security.LoginUser;
import com.wondertek.common.util.security.SecurityUtil;
import com.wondertek.service.dto.business.res.DeviceErrorRecordMessageRes;
import com.wondertek.service.entity.business.DeviceErrorRecordMessagePo;
import com.wondertek.service.mapper.business.DeviceErrorRecordMessageMapper;
import com.wondertek.service.service.business.DeviceErrorRecordMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wangyu
 */
@Service
public class DeviceErrorRecordMessageServiceImpl extends ServiceImpl<DeviceErrorRecordMessageMapper, DeviceErrorRecordMessagePo> implements DeviceErrorRecordMessageService {

    @Autowired
    private DeviceErrorRecordMessageMapper deviceErrorRecordMessageMapper;

    /**
     * 根据userId查询告警站内消息
     * @return
     */
    @Override
    public Page<DeviceErrorRecordMessageRes> findMessageByUserId(Integer pageNo,Integer pageSize) {

        LoginUser loginUser = SecurityUtil.getLoginUser();
        String userId = loginUser != null ? loginUser.getUserId() : null;

        if (StringUtil.isEmpty(userId))
            throw new AppException("user.id.not.null");

        return deviceErrorRecordMessageMapper.findMessageByUserId(userId,new Page<>(pageNo,pageSize));
    }

    /**
     * 根据id修改告警站内消息已读状态
     * @param id
     */
    @Override
    public void updateMessageReadById(String id) {

        DeviceErrorRecordMessagePo deviceErrorRecordMessagePo = new DeviceErrorRecordMessagePo();
        deviceErrorRecordMessagePo.setId(id);
        deviceErrorRecordMessagePo.setIsRead(1);

        this.updateById(deviceErrorRecordMessagePo);

    }
}
